package com.swa99ylum.pjv.validator;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

import com.swa99ylum.pjv.exception.JSONValidateException;

/** <h1>日期格式 验证器</h1>
<br> 1、验证 日期字符串 是否 匹配 日期
<br> 2、pattern 的用法和 SimpleDateFormat 完全一样
<br>    
 */
public class DateFormatValidator extends Validator {
	
	
	private String pattern;

	public DateFormatValidator(String pattern) {
		super();
		this.pattern = pattern;
	}
	
	@Override
	public void doValidate(Object rootObject, String[] nodePath, String nodeName, Map<String, Object> parentValue, Object value) throws JSONValidateException {
		DateFormat df = new SimpleDateFormat(pattern);

		
		try {
			df.parse(String.valueOf(value));
		} catch (ParseException e) {
			throw new JSONValidateException(nodePath, value, "不符合 日期格式：" + pattern); 
		}
		
	}
	
	public static void main(String[] args) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		
		try {
			df.parse("20180201");
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	
}
